package proxy;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.UiAutomation;
import android.content.Context;
import android.graphics.Point;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.util.SparseArray;
import android.view.Display;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityWindowInfo;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import mirror.android.app.ActivityThread;
import mirror.android.hardware.display.DisplayManagerGlobal;

import org.apache.commons.io.IOUtils;

import proxy.wrappers.ClipboardManager;
import proxy.wrappers.ServiceManager;
import uiautomator.AccessibilityNodeInfoDumper;
import uiautomator.InstrumentShellWrapper;
import utils.compat.BuildCompat;

/* loaded from: classes2.dex */
public class Bridge {
    public static final int BATTERY_STATUS_CHARGING = 2;
    public static final int BATTERY_STATUS_DISCHARGING = 3;
    public static final int BATTERY_STATUS_FULL = 5;
    public static final int BATTERY_STATUS_NOT_CHARGING = 4;
    public static final int BATTERY_STATUS_UNKNOWN = 1;
    private static Bridge sInstance;
    Object activityThread;
    BatteryManager batteryManager;
    Context context;
    Object displayManager;
    ServiceManager serviceManager = new ServiceManager();

    public static class BatteryInfo {
        public int capacity;
        public int chargeCounter;
        public int currentAverage;
        public int currentNow;
        public boolean isCharging;
    }

    private Bridge() {
    }

    public static Bridge getInstance() {
        if (sInstance == null) {
            sInstance = new Bridge();
        }
        return sInstance;
    }

    public int[] getDisplayIds() {
        if (this.displayManager == null) {
            this.displayManager = DisplayManagerGlobal.getInstance.call(new Object[0]);
        }
        int[] displayIds = DisplayManagerGlobal.getDisplayIds.call(this.displayManager, new Object[0]);
        return displayIds;
    }

    Display getDisplayById(int displayId) {
        if (this.displayManager == null) {
            this.displayManager = DisplayManagerGlobal.getInstance.call(new Object[0]);
        }
        return DisplayManagerGlobal.getRealDisplay.call(this.displayManager, Integer.valueOf(displayId));
    }

    public int getDisplayDensityDpi(int displayId) {
        Object displayManager = DisplayManagerGlobal.getInstance.call(new Object[0]);
        Display display = DisplayManagerGlobal.getRealDisplay.call(displayManager, Integer.valueOf(displayId));
        DisplayMetrics metrics = new DisplayMetrics();
        display.getRealMetrics(metrics);
        return metrics.densityDpi;
    }

    public DisplayMetrics getDisplayMetrics(int displayId) {
        Object displayManager = DisplayManagerGlobal.getInstance.call(new Object[0]);
        Display display = DisplayManagerGlobal.getRealDisplay.call(displayManager, Integer.valueOf(displayId));
        DisplayMetrics metrics = new DisplayMetrics();
        display.getRealMetrics(metrics);
        return metrics;
    }

    public String dumpWindows(int displayId, boolean verboseMode) throws AccessibilityNodeInfoDumper.DumpWindowException, IOException {
        UiAutomation uiAutomation = InstrumentShellWrapper.getInstance().getUiAutomation();
        if (verboseMode) {
            InstrumentShellWrapper.getInstance().setCompressedLayoutHierarchy(false);
        } else {
            InstrumentShellWrapper.getInstance().setCompressedLayoutHierarchy(true);
        }
        AccessibilityServiceInfo info = uiAutomation.getServiceInfo();
        info.flags |= 64;
        uiAutomation.setServiceInfo(info);
        SparseArray<List<AccessibilityWindowInfo>> allWindows = uiAutomation.getWindowsOnAllDisplays();
        if (allWindows.size() == 0) {
            throw new AccessibilityNodeInfoDumper.DumpWindowException("windows empty");
        }
        int nd = allWindows.size();
        for (int d = 0; d < nd; d++) {
            if (allWindows.keyAt(d) == displayId) {
                return AccessibilityNodeInfoDumper.dumpWindows(allWindows.valueAt(d), displayId);
            }
        }
        throw new AccessibilityNodeInfoDumper.DumpWindowException("display not found");
    }

    public String dumpXml(boolean allWindows, boolean verboseMode) {
        InstrumentShellWrapper.getInstance().setCompressedLayoutHierarchy(!verboseMode);
        UiAutomation uiAutomation = InstrumentShellWrapper.getInstance().getUiAutomation();
        if (allWindows && BuildCompat.isR()) {
            AccessibilityServiceInfo info = uiAutomation.getServiceInfo();
            info.flags |= 64;
            uiAutomation.setServiceInfo(info);
            return AccessibilityNodeInfoDumper.dumpWindows(uiAutomation.getWindowsOnAllDisplays());
        }
        Object displayManager = DisplayManagerGlobal.getInstance.call(new Object[0]);
        Display display = DisplayManagerGlobal.getRealDisplay.call(displayManager, 0);
        int rotation = display.getRotation();
        Point size = new Point();
        display.getRealSize(size);
        AccessibilityNodeInfo info2 = uiAutomation.getRootInActiveWindow();
        if (info2 == null) {
            System.err.println("ERROR: null root node returned by UiTestAutomationBridge.");
            return JsonProperty.USE_DEFAULT_NAME;
        }
        return AccessibilityNodeInfoDumper.dumpWindow(info2, rotation, size.x, size.y);
    }

    public BatteryInfo getBatteryInfo() {
        if (this.batteryManager == null) {
            this.batteryManager = mirror.android.os.BatteryManager.ctor.newInstance();
        }
        int capctity = this.batteryManager.getIntProperty(4);
        int average = this.batteryManager.getIntProperty(3);
        int current = this.batteryManager.getIntProperty(2);
        int counter = this.batteryManager.getIntProperty(1);
        BatteryInfo info = new BatteryInfo();
        info.capacity = capctity;
        info.currentAverage = average;
        info.chargeCounter = counter;
        info.currentNow = current;
        if (BuildCompat.isM()) {
            info.isCharging = this.batteryManager.isCharging();
        }
        return info;
    }

    public Context getContext() {
        if (this.activityThread == null) {
            if (Looper.myLooper() == null) {
                Looper.prepareMainLooper();
            }
            this.activityThread = ActivityThread.systemMain.call(new Object[0]);
            this.context = ActivityThread.getSystemContext.call(this.activityThread, new Object[0]);
        }
        return this.context;
    }

    public ClipboardManager getClipboardManager() {
        return this.serviceManager.getClipboardManager();
    }

    public static byte[] takeScreenshot() throws IOException, InterruptedException {
        Process proc = null;
        try {
            try {
                ProcessBuilder processBuilder = new ProcessBuilder("screencap", "-p");
                proc = processBuilder.start();
                byte[] pngBytes = IOUtils.toByteArray(proc.getInputStream());
                if (Build.VERSION.SDK_INT >= 26) {
                    proc.waitFor(5000L, TimeUnit.MILLISECONDS);
                } else {
                    proc.waitFor();
                }
                return pngBytes;
            } catch (IOException | InterruptedException e) {
                throw e;
            }
        } finally {
            if (proc != null) {
                try {
                    proc.getInputStream().close();
                    proc.getOutputStream().close();
                    proc.getErrorStream().close();
                } catch (IOException e2) {
                }
            }
        }
    }
}